package com.octopus.optiworks.service;

import java.util.List;
import java.util.Locale;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ConfigurationService {
	
	private static Log log = LogFactory.getLog(ConfigurationService.class);
	
	//Configuration for rework
	private String configureIsReworkKey = "step.rework";
	private boolean configureIsReworkValue = false;
	//Configuration for auto create sn
	private String configureAutoCreateSnKey = "step.finalsn.auto";
	private boolean configureAutoCreateSnValue = false;
	//Configuration for auto operator/date
	private String configureAutoOperatorDateKey = "step.operator.date.auto";
	private boolean configureAutoOperatorDateValue = false;
	//Configuration for default values
	private String configureDefaultValueKey = "default.value";
	private String[] configureDefaultValueValue = new String[]{};
	
	private static ConfigurationService configurationService;
	
	private ConfigurationService(){
		String str = this.getConfigurationProperty(configureIsReworkKey);
		if(str != null && str.equals("true"))
			configureIsReworkValue = true;
		str = this.getConfigurationProperty(configureAutoCreateSnKey);
		if(str != null && str.equals("true"))
			configureAutoCreateSnValue =  true;
		str = this.getConfigurationProperty(configureAutoOperatorDateKey);
		if(str != null && str.equals("true"))
			configureAutoOperatorDateValue =  true;
		str = this.getConfigurationProperty(configureDefaultValueKey);
		if(str != null && str.length() > 0)
			configureDefaultValueValue = str.split(",");
	}
	
	public static synchronized ConfigurationService getInstance(){
		if(configurationService == null)
			configurationService = new ConfigurationService();
		return configurationService;
	}
	
	public boolean isRework() {
		return configureIsReworkValue;
	}
	
	public boolean isAutoCreateSn() {
		return configureAutoCreateSnValue;
	}
	
	public boolean isAutoOpeartorDate(){
		return configureAutoOperatorDateValue;
	}
	
	public String[] getDefaultValueFields(){
		return configureDefaultValueValue;
	}
	
	public String getConfigurationProperty(String key){
		ResourceBundle configuation = ResourceBundle.getBundle("configuration", Locale.getDefault());
		try{
			String value = configuation.getString(key);
			return value;
		}catch(MissingResourceException e){
			log.warn("Missing configuration for the key: " + key);
		}
		return null;
	}

}
